
package org.brisling.base.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.common.util.JsonUtil;
import org.brisling.sysman.domain.JobRole;
import org.brisling.sysman.domain.Permissions;
import org.brisling.sysman.domain.Users;

/**
 * 用户权限检测工具类
 * <p>Title: PermissionCheck</p>
 * <p>Description: </p>
 * <p>Company: tongking</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2016年11月27日 上午11:07:07
 */
public class PermissionCheck {

	 
	private static PermissionCheck pCheck;
	
	private PermissionCheck(){}
	
	public static PermissionCheck getInstance(){
		if(null == pCheck){
			pCheck = new PermissionCheck();
		}
		return pCheck;
	}
	
	
	/**
	 * 检查用户是否隶属于超级管理部门
	 * @param userid		用户id
	 * @param allDept		超级管理部门id
	 * @param subModuleAllDept	模块超级管理部门id
	 * @return	true:用户属于超级管理;false:用户未隶属于超级管理部门
	 */
	public boolean isSuperDept(String userid,String allDept,String subModuleAllDept){
		
		boolean ret = false;
		if(userid!=null){
			
			//判断是否管理用户
			String _sid = SysConfig.getConfig("superUserIdentify");
			if(_sid !=null && _sid.compareTo(userid)==0){
				ret = true;
			}
			
			//判断超级管理部门与当前用户部门是否相同
			String queryAll = SysConfig.getConfig(allDept) +","+ SysConfig.getConfig(subModuleAllDept);
			Users u =  SysConfig.getUsr(userid);
			
			if(u!=null && u.getDeptid()!=null){
				String curDept =u.getDeptid().toString();
				
				if(queryAll!=null && curDept!=null){
					String[] sd = queryAll.split(",");
					
					curDept = curDept.trim();
					//迭代超级管理部门id
					for(String did:sd){
						//判断迭代数据是否有效
						if(did!=null){
							
							//部门匹配成功，中断循环，返回判断匹配标志
							if(did.trim() .compareTo(curDept)==0){
								ret = true;
								break;
							}
						}						
					}
				}
			}
		}
		
		return ret;
	}
	
	
	/**
	 * 检查是否管理角色
	 * @param allDept
	 * @param subModuleAllDept
	 * @return
	 */
	public boolean isManageJob(String userid){
		
		boolean retVal = false;
		Users usr = SysConfig.getUsr(userid);
		if((usr==null)||(usr.getJobrole()==null)||(usr.getJobrole().size()<=0))
			return retVal;
		
		List<JobRole> jobrole = usr.getJobrole();
		for(JobRole jr:jobrole){
			if((jr.getJobcode()!=null )&&(jr.getJobcode()<=40)){
				retVal = true;
				break;
			}
		}
		
		return retVal;
	}
	
	/**
	 * 检查是否业务管理部门
	 * @param userid	用户id
	 * @param fullBizcode	完整业务编码
	 * @return
	 */
	public boolean isBizManageDept(String userid,String fullBizcode){
		
		boolean ret = false;
		//参数有效性判断
		if(userid ==null || fullBizcode ==null){
			return false;
		}
		
		//获取用户信息
		Users usr = SysConfig.getUsr(userid);
		
		String userDeptid = usr.getDeptid().toString();
		//判断用户是否管理角色
//		if(!this.isManageJob(userid)){
//			return false;
//		}
		
		Map<String, Permissions> maps = SysConfig.getFlowBizMap();
		
		Permissions p = null;
		if(null !=maps && maps.size()>0){
			//获取fullbizcode所对应业务配置
			if(maps.containsKey(fullBizcode)){
				p = maps.get(fullBizcode);
				
				if((null != p) && (p.getAddition_param()!=null)){
					//获取模块附加参数
					String param = p.getAddition_param().trim();
					Map<String,String> paramMap =JsonUtil.bodyToObj(param, HashMap.class);
					
					//获取模块管理部门id
					if((paramMap != null)&&(paramMap.containsKey(ConstSysParam.BIZ_MANAGE_DEPT))){
						String deptids = paramMap.get(ConstSysParam.BIZ_MANAGE_DEPT);
						
						//循环迭代判断用户部门id是否与模块业务管理部门id相同
						if(deptids!=null){
							String[] idArray = deptids.split(",");
							for(String _id:idArray){
								if(_id!=null){
									_id = _id.trim();
									if(userDeptid.compareTo(_id)==0){
										ret = true;
										break;
									}
								}
							}
						}
					}
				}
			}
		}
		return ret;
	}
	
	/**
	 * 获取用户管理的部门
	 * @return
	 */
	public String getJobDeptids(String userid){
		Users usr =SysConfig.getUsr(userid);
		String deptids = null;
		if(usr!=null){
			List<JobRole> lists =usr.getJobrole();
			if(lists!=null && lists.size()>0){
				for (JobRole r:lists){
					String did = r.getDeptid().toString();
					deptids = deptids ==null?did:deptids + ","+did;
				}
			}else{
				if(usr.getDeptid()!=null)
					deptids = usr.getDeptid().toString();
			}
		}
		
		return deptids;
	}
	
	/**
	 * 获取用户管理的部门
	 * @param userid	用户id
	 * @return
	 */
	public String getJobDeptidAndAccountDeptids(String userid){
		Users usr =SysConfig.getUsr(userid);
		String deptids = null;
		if(usr!=null){
			List<JobRole> lists =usr.getJobrole();
			if(lists!=null && lists.size()>0){
				for (JobRole r:lists){
					Long did = r.getDeptid();
					Long accountDeptid = SysConfig.getDept(did).getAccounting_deptid();
					String dids = did + "," +accountDeptid;
					deptids = deptids ==null?dids+"":deptids + ","+dids;
				}
			}else{
				if(usr.getDeptid()!=null)
					deptids = usr.getDeptid().toString();
			}
		}
		
		return deptids;
	}
}
